package se.webbzon.oschi01.action;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import se.webbzon.oschi01.command.History;
import se.webbzon.oschi01.command.HistoryListener;
import se.webbzon.oschi01.gui.ApplicationIconSheet;
import se.webbzon.oschi01.gui.ApplicationIcon;
import se.webbzon.oschi01.singleton.Singletons;

public class RedoAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -960121939259248813L;
	
	private static final ApplicationIconSheet iconSheet = Singletons.instance(ApplicationIconSheet.class);
	
	private final History history;
	
	public RedoAction(History history) {
		this.history = history;
		putValue(NAME, "Redo");
		putValue(SMALL_ICON, iconSheet.getIcon(ApplicationIcon.Redo));
		setEnabled(history.canRedo());
		history.addListener(new HistoryListenerImpl());
	}

	@Override public void actionPerformed(ActionEvent e) {
		if (history.canRedo())
			history.redo();
	}
	
	private final class HistoryListenerImpl implements HistoryListener {

		@Override public void onHistoryUndo(History history) {
			setEnabled(history.canRedo());
		}

		@Override public void onHistoryRedo(History history) {
			setEnabled(history.canRedo());
		}
		
	}

}
